import axios from "axios";
//创建axios实例
const service=axios.create({
    //url开头
    baseURL:"api",
    timeout: 20000,
    //请求头配置 
    headers:{
        "Content-Type": "application/json"
    }
})
//请求拦截
service.interceptors.request.use(
    (config)=>{
    //请求头放token
    config.headers=config.headers || {}
    if(localStorage.getItem('token')){
        config.headers.Authorization=localStorage.getItem('token') || ""
    }
    return config;
},
error => {
    // ...
    return Promise.reject(error);
  }
)
//响应拦截 
service.interceptors.response.use((res)=>{
    const code:number=res.data.code
    // 如果响应code不为200拦截掉
    if(code!=200){
        return Promise.reject(res.data)
    }
    return res.data;
},(err)=>{
     // 打印错误请求
    console.log(err.msg);
})

export default service